import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

# 支持中文
plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']
# 支持负数
plt.rcParams['axes.unicode_minus'] = False

def create_polar_system(r_range=(0, 5), figsize=(8, 8)):
    """
    创建极坐标系
    """
    fig, ax = plt.subplots(figsize=figsize, subplot_kw=dict(projection='polar'))
    
    # 设置极径范围
    ax.set_ylim(0, r_range[1])
    
    # 设置极角刻度标签
    ax.set_theta_zero_location('E')  # 0度在右边
    ax.set_theta_direction(1)       # 逆时针为正方向
    
    # 设置极径刻度
    ax.set_rticks(np.arange(0, r_range[1]+1, 1))
    
    # 添加网格
    ax.grid(True)
    
    ax.set_title('极坐标系', fontsize=16, pad=20)
    
    return fig, ax

if __name__ == "__main__":
    # 创建极坐标系
    fig, ax = create_polar_system(r_range=(0, 5))
    
    # 添加45度角的案例
    theta_45 = np.deg2rad(45)  # 45度转换为弧度
    r_value = 3.5  # 极径值
    
    # 绘制45度方向的点
    ax.plot(theta_45, r_value, 'ro', markersize=8, label='45°点')
    
    # 绘制45度方向的射线
    ax.plot([theta_45, theta_45], [0, 5], 'r--', linewidth=1.5, label='45°射线')
    
    # 绘制45度方向的圆
    theta_circle = np.linspace(0, 2*np.pi, 100)
    r_circle = np.full_like(theta_circle, r_value)
    ax.plot(theta_circle, r_circle, 'b-', linewidth=1.5, label=f'r={r_value}的圆')
    
    # 添加图例
    ax.legend(loc='upper right', bbox_to_anchor=(1.15, 1.15))
    
    # 添加45度角标签
    ax.text(theta_45 + 0.05, r_value + 0.2, '45°', fontsize=12, color='red')
    
    plt.show()